package com.luoran.car.controller;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.luoran.car.domain.SysCarComplaints;
import com.luoran.car.domain.SysTradesmanCity;
import com.luoran.car.service.SysCarComplaintsService;
import com.luoran.car.service.SysTradesmanCityService;
import com.luoran.car.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin(origins = "http://localhost:8080", allowCredentials = "true")
@RestController
@RequestMapping("complanints")
public class ComplanintsController {


    @Autowired
    SysCarComplaintsService complaintsService;

    @Autowired
    SysTradesmanCityService tradesmanCityService;

    @GetMapping("complanintsList")
    public Result complanintsList() {
        List<SysCarComplaints> list = complaintsService.complanintsList();
        return Result.success(list);
    }

    @PostMapping("complanintsInsert")
    public Result complanintsInsert(@RequestBody SysCarComplaints carComplaints) {
        Boolean bool = complaintsService.complanintsInsert(carComplaints);
        return bool?Result.success():Result.fail();
    }


    @GetMapping("getAreaTree")
    public Result getAreaTree() {

        List<SysTradesmanCity> list = tradesmanCityService.list();

        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();

// 最大递归深度
        treeNodeConfig.setDeep(3);

//转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.<SysTradesmanCity, String>build(list, "1", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId()+"");
                    tree.setParentId(treeNode.getPid()+"");
                    tree.setName(treeNode.getCityName()+"");
                });

        return Result.success(treeNodes);

    }


    @GetMapping("complanintsDel")
    public Result complanintsDel(Integer id) {
        Boolean bool = complaintsService.complanintsDel(id);
        return bool?Result.success():Result.fail();
    }

    @PostMapping("complanintsUpdata")
    public Result complanintsUpdata(@RequestBody SysCarComplaints carComplaints) {
        Boolean bool = complaintsService.complanintsUpdata(carComplaints);
        return bool?Result.success():Result.fail();
    }
}
